<?php
	/**
	 * 获取战绩列表
	 */
    class CombatGains extends APIBase {
		
		public $tag = "CombatGains";
    	public $isLogin = true;
		
		public function before() {
			$this->initCacheRedis();
			$this->initMysql();
			return true;
		}
		public function logic() {

			//$ann = $this->cache_redis->hMGet("ha:*", array("id","title","content","image_url"));
			//$combatgains= $this->cache_redis->keys("hCombatGains:*");
			$uid =  $this->uid;
			$UserCombatgains = $this->cache_redis->keys("hUserCombatgains:$uid:*");
			if(!empty($UserCombatgains)){
				$count = count($UserCombatgains);
				$result = array();
			    if ($count) {
				    foreach($UserCombatgains as $key=>$val){
					 $result[] = $this->cache_redis->hGetAll("$val"); 
				    }
	            }
				//$result['playerID'] = $uid;
				//echo "<pre>";
				//var_dump($result);exit;
				$this->returnData($result);
			}else{
		        $sql = "select * from customgamelog where playerID = $uid GROUP BY deskBuildDate";
				$result = $this->mysql->query($sql);
				$data = array();
				$deskBuildDate = array();
				$deskCode = array();
				$pattern = array();
				//echo "<pre>";
			    if(!empty($result)){
			        while ( $row = mysql_fetch_assoc($result) ) {
			            $rows[] = $row;
		            }
				    if(!empty($rows)){
						foreach($rows as $k=>$v){
							if((strtotime(date("Y-m-d",time())) - strtotime(date("Y-m-d",$v['deskBuildDate'])))/24/3600 < 7){
								$deskBuildDate[] = $v['deskBuildDate']; 
							    $deskCode[] = $v['deskCode'];
								$pattern[] = $v['pattern'];
							}
						}
				    }else{
						$this->returnError(301,'暂无战绩记录！');
					}
	            }else{
					$this->returnError(301,'暂无战绩记录！');
				}

				if(!empty($deskBuildDate)){
					$arr = array();
					for($i=0;$i<count($deskBuildDate);$i++){
						$result_ = $this->mysql->select("customgamelog","SUM(score) as total,playerID,deskCode",array('deskBuildDate'=>$deskBuildDate[$i]),'GROUP BY playerID');							
						$userinfo = array();
						foreach($result_ as $v){
							//$user = $this->mysql->select('player','user,avater_url,id,name',array('id'=>$v['playerID']));
							$user = $this->data_redis->hMget("hu:".$v['playerID'],array('name','avater_url','id'));
							/*
							if(mb_strlen($user['name'],'utf-8') > 4){
					            $user['name'] = mb_substr($user['name'],0,4,'utf-8').'...';
				            }
							*/
							$userinfo[] = array('point'=>$v['total'],'name'=>$user['name'],'pic'=>$user['avater_url'],'playerID'=>$user['id'],'deskCode'=>$v['deskCode']);
						}
						$date = $deskBuildDate[$i];
						$desk = $deskCode[$i];
						$patterns = $pattern[$i];
						$row = $this->mysql->find("select gameDate as counts FROM customgamelog where deskBuildDate=$date and deskCode=$desk  GROUP BY gameDate");
						$playtotal = count($row);
                        $arr[$i] = array('deskCode'=>$deskCode[$i],'userinfo'=>$userinfo,'deskBuildDate'=>$deskBuildDate[$i],'pattern'=>$patterns,'playtotal'=>$playtotal);
						//$this->cache_redis->hMset("hUserCombatgains:".$uid.":$date:$desk",array('deskCode'=>$deskCode[$i],'userinfo'=>json_encode($userinfo),'deskBuildDate'=>$deskBuildDate[$i]));
					}

					for($k=1;$k<count($arr);$k++){
			            for($i=0;$i<count($arr)-$k;$i++){
				            $temp = '';
				            if($arr[$i]['deskBuildDate'] < $arr[$i+1]['deskBuildDate']){
					            $temp = $arr[$i];
					            $arr[$i] = $arr[$i+1];
					            $arr[$i+1] = $temp;
				            }
			            }
			        }
					$this->returnData($arr);
				}else{
					$this->returnError(301,'最近7天暂无战绩记录！');
				}
			}

		}
		
        public function after() {
			$this->deinitCacheRedis();
		}
    }
?>
